/**
 * @ author xc
 * @date 2018年7月4日 下午3:31:50
 * @ version 1.0
 */
package com.yidu.menuProject.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.menuProject.action.vo.EhyManagerVo;
import com.yidu.menuProject.action.vo.EhyModuleVo;
import com.yidu.menuProject.common.Page;
import com.yidu.menuProject.common.Tools;
import com.yidu.menuProject.model.EhyManagerModel;
import com.yidu.menuProject.service.EhyManagerService;


/**
 * EhyManager与前端交互的Action
 * @author Administrator
 */
@Controller
@RequestMapping("/userAction")
public class EhyManagerAction {
	@Resource
	private EhyManagerService ehymanagerService ;
	
	@RequestMapping("/isLogin")
	public String isLogin(String manUser,String manPwd,String yzm,HttpServletRequest request){
		EhyManagerModel ehymanagermodel = ehymanagerService.isLogin(manUser, manPwd);
		String str = (String) request.getSession().getAttribute("setfood");
		if(ehymanagermodel!=null || yzm.equals(str)){
			HttpSession session = request.getSession();
			session.setAttribute("ehymanagermodel", ehymanagermodel);
			return "redirect:/pages/html/managerHome.html";
		}else{ 
			request.setAttribute("message", "用户名或密码错误");
			return "redirect:/pages/food/login.html";
		}
	}
	
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpSession session,EhyManagerVo vo){
		EhyManagerVo ehymanger = ehymanagerService.findCustomer(vo.getManUser(), vo.getManPwd());
		EhyManagerModel ehymanagermodel = ehymanagerService.isLogin(vo.getManUser(), vo.getManPwd());
		if(ehymanger==null && ehymanger.getIsva()==1){
			session.setAttribute("msg", "密码或用户名错误！");
			return "redirect:/pages/pages_xc/404.html";
		}else{
			List<EhyModuleVo> list = ehymanagerService.findModule(vo.getManUser(), vo.getManPwd());
			session.setAttribute("user", ehymanger);
			session.setAttribute("module", list);
			vo = null;
			if(ehymanger.getMealId()==null){
			return "redirect:/pages/pages_xc/index.jsp";
			}else{
				session.setAttribute("ehymanagermodel", ehymanagermodel);
				session.setAttribute("mana", ehymanger);
				return "redirect:/pages/html/managerHome.html";
			}
		}
		
	}
	
	@RequestMapping("/selectAll")
	@ResponseBody
	public  Map<String, Object>  showList(EhyManagerVo vo,int page,int limit) {
		EhyManagerVo mod = new EhyManagerVo();
		vo.setNowpage(page);
		vo.setRows(limit);
		List<EhyManagerModel> list = ehymanagerService.selectAll(vo);
		Integer count = ehymanagerService.selectCount(mod);
		Map<String, Object> map=new HashMap<>();
		map.put("code", 0);
		map.put("msg", null);
		map.put("count", count);
		map.put("data", list);
	return map;
		
	}
	
	@RequestMapping("/getPages")
	@ResponseBody
	public Page getPages(EhyManagerVo vo){
		Page p = new Page();
		int count = ehymanagerService.selectCount(vo);
		p.setTotalRows(count);
		return p;
	}	
	
	/**
	 * 增加或修改
	 * @throws IOException 
	 */
	@RequestMapping("/updateUser")
	public String updateModule(EhyManagerVo vo) {
		
		ehymanagerService.updateByPrimaryKeySelective(vo);
		vo = new EhyManagerVo();
		return "redirect:/pages/pages_xc/ManagerList.html";
		
		
		
	}
	
	@RequestMapping("/addUser")
	public String addModule(EhyManagerVo vo){
		vo.setManId(Tools.getStringRandom(10));
		vo.setIsva(2);
		ehymanagerService.insertSelective(vo);
		vo = new EhyManagerVo();
		return "redirect:/pages/pages_xc/ManagerList.html";
		
	}
	/**
	 * 删除用户
	 * @return
	 * @throws IOException 
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(String manId){
		 
		 ehymanagerService.deleteByPrimaryKey(manId);
		
		return "pages_xc/ManagerList" ;
	}
	
	
	
	
	
	
	
	
}

